package vistas;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import clases.Alumno;
import clases.Seccion;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JDAlumno extends javax.swing.JDialog {
	private JPanel jpAlumno;
	private JLabel jLabelCedula;
	private AbstractAction abstractAction4;
	private AbstractAction abstractAction3;
	private JLabel jLabelApellido;
	private JTextField jTextNombre;
	private JTextField jTextApellido;
	private AbstractAction abstractAction1;
	private AbstractAction abstractAction2;
	private JButton jButtonRegistrar;
	private JButton jButtonEliminar;
	private JButton jButtonSalir;
	private JButton jButtonBuscar;
	private JTextField jTextCedula;
	private JLabel jLabelNombre;
	private Seccion _seccion;
	
	
	
	/**
	* Auto-generated main method to display this JDialog
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				JDAlumno inst = new JDAlumno(frame, new Seccion());
				inst.setVisible(true);
			}
		});
	}
	
	public JDAlumno(JFrame frame,Seccion secion) {
		super(frame);
		initGUI();
		_seccion = secion;
	}
	
	
	private void initGUI() {
		try {
			setTitle("Registro del Alumno");
			{
				jpAlumno = new JPanel();
				getContentPane().add(jpAlumno, BorderLayout.CENTER);
				jpAlumno.setLayout(null);
				{
					jLabelCedula = new JLabel();
					jpAlumno.add(jLabelCedula);
					jLabelCedula.setText("Cedula:");
					jLabelCedula.setBounds(17, 36, 77, 14);
				}
				{
					jLabelNombre = new JLabel();
					jpAlumno.add(jLabelNombre);
					jLabelNombre.setText("Nombre:");
					jLabelNombre.setBounds(17, 91, 77, 14);
				}
				{
					jLabelApellido = new JLabel();
					jpAlumno.add(jLabelApellido);
					jLabelApellido.setText("Apellido:");
					jLabelApellido.setBounds(12, 145, 82, 14);
				}
				{
					jTextCedula = new JTextField();
					jpAlumno.add(jTextCedula);
					jTextCedula.setBounds(112, 29, 89, 21);
				}
				{
					jTextNombre = new JTextField();
					jpAlumno.add(jTextNombre);
					jTextNombre.setBounds(112, 84, 89, 21);
				}
				{
					jTextApellido = new JTextField();
					jpAlumno.add(jTextApellido);
					jTextApellido.setBounds(112, 142, 89, 21);
				}
				{
					jButtonBuscar = new JButton();
					jpAlumno.add(jButtonBuscar);
					jButtonBuscar.setText("Buscar");
					jButtonBuscar.setBounds(236, 29, 82, 21);
					jButtonBuscar.setAction(getAbstractAction3());
				}
				{
					jButtonSalir = new JButton();
					jpAlumno.add(jButtonSalir);
					jButtonSalir.setText("Volver");
					jButtonSalir.setBounds(265, 221, 106, 21);
					jButtonSalir.setAction(getAbstractAction4());
					jButtonSalir.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							System.out.println("jButtonSalir.actionPerformed, event="+evt);
							//TODO add your code for jButtonSalir.actionPerformed
							//System.exit(0);	
						}
					});
				}
				{
					jButtonEliminar = new JButton();
					jpAlumno.add(jButtonEliminar);
					jButtonEliminar.setText("Eliminar");
					jButtonEliminar.setBounds(149, 221, 105, 21);
					jButtonEliminar.setAction(getAbstractAction2());
				}
				{
					jButtonRegistrar = new JButton();
					jpAlumno.add(jButtonRegistrar);
					jButtonRegistrar.setText("Registrar");
					jButtonRegistrar.setBounds(29, 221, 109, 21);
					jButtonRegistrar.setAction(getAbstractAction1());
				}
			}
			setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private AbstractAction getAbstractAction1() {
		if(abstractAction1 == null) {
			abstractAction1 = new AbstractAction("Registrar", null) {
				public void actionPerformed(ActionEvent evt) {
					Vector<JTextField> Jtextos = new Vector<JTextField>();
					Jtextos.add(jTextCedula);
					Jtextos.add(jTextNombre);
					Jtextos.add(jTextApellido);
					for(JTextField text: Jtextos){
						if(text.getText().equals("")){
							JOptionPane.showMessageDialog(new JFrame(),"Debe llenar todos los campos","Registro del Alumno",JOptionPane.WARNING_MESSAGE);
							return;
						}
					}
					if(_seccion.buscar(jTextCedula.getText())){
						JOptionPane.showMessageDialog(new JFrame(),"La cedula ya se encuentra registrada","Registro del Alumno",JOptionPane.WARNING_MESSAGE);
						return;
					}
					_seccion.inscribirAlumno(new Alumno(jTextCedula.getText(),jTextNombre.getText(),jTextApellido.getText()));
					JOptionPane.showMessageDialog(new JFrame(),"Alumno registrado con exito","Registro del Alumno",JOptionPane.INFORMATION_MESSAGE);
					for(JTextField text: Jtextos){
						text.setText("");
					}
				}
			};
		}
		return abstractAction1;
	}
	
	private AbstractAction getAbstractAction2() {
		if(abstractAction2 == null) {
			abstractAction2 = new AbstractAction("Eiminar", null) {
				public void actionPerformed(ActionEvent evt) {
					Vector<JTextField> Jtextos = new Vector<JTextField>();
					Jtextos.add(jTextCedula);
					Jtextos.add(jTextNombre);
					Jtextos.add(jTextApellido);
					if(jTextCedula.getText().equals("")){
							JOptionPane.showMessageDialog(new JFrame(),"Debe llenar el campo cedula","Registro del Alumno",JOptionPane.WARNING_MESSAGE);
							return;
					}	
					Alumno a = _seccion.retirarAlumno(jTextCedula.getText());
					if(a!=null){
						JOptionPane.showMessageDialog(new JFrame(),"Alumno eliminado con exito","Registro del Alumno",JOptionPane.WARNING_MESSAGE);
					}
					else
						JOptionPane.showMessageDialog(new JFrame(),"La cedula ingresada no pertenece a ningun alumno","Registro del Alumno",JOptionPane.INFORMATION_MESSAGE);
					for(JTextField text: Jtextos){
						text.setText("");
					}
				}
			};
		}
		return abstractAction2;
	}
	
	private AbstractAction getAbstractAction3() {
		if(abstractAction3 == null) {
			abstractAction3 = new AbstractAction("Buscar", null) {
				public void actionPerformed(ActionEvent evt) {
					if(jTextCedula.getText().equals("")){
							JOptionPane.showMessageDialog(new JFrame(),"Debe llenar el campo cedula","Registro del Alumno",JOptionPane.WARNING_MESSAGE);
							return;
					}	
					Alumno a = _seccion.retirarAlumno(jTextCedula.getText());
					if(a!=null){
						jTextCedula.setText(a.get_cedula());
						jTextNombre.setText(a.get_nombre());
						jTextApellido.setText(a.get_apellido());
					}
					else
						JOptionPane.showMessageDialog(new JFrame(),"La cedula ingresada no pertenece a ningun alumno","Registro del Alumno",JOptionPane.INFORMATION_MESSAGE);
				}
			};
		}
		return abstractAction3;
	}
	
	private AbstractAction getAbstractAction4() {
		if(abstractAction4 == null) {
			abstractAction4 = new AbstractAction("Volver", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return abstractAction4;
	}

}
